package com.ruisoft.business.attendance.controller;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ruisoft.base.controller.BaseController;
import com.ruisoft.business.attendance.service.AttendService;
import com.ruisoft.core.json.JSONMap;

@Controller
@RequestMapping("/attendance/")
public class AttendController  extends BaseController  {
	private static final Logger LOG = Logger.getLogger(AttendController.class);
	
	@Autowired
	private AttendService attendService;
	
	@RequestMapping("saveMachinfo")
	public void saveMachinfo() {
		JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
		try {
			JSONObject reqData = getReqData();
			JSONObject result = new JSONObject();
			result = attendService.saveMachinfo(reqData);
			json.put("result", result);
			json.put("status", "1");
		} catch (Exception e) {
			LOG.error("保存考勤设备信息发生错误", e);
			json.put("status", "-2");
			json.put("msg", "保存考勤设备信息发生错误");
		} finally {
			try {
				response(json);
			} catch (IOException e) {
				LOG.error("返回信息时发生错误", e);
			}
		}
	}
	
	@RequestMapping("managequery")
	public void managequery() {
		JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
		if (queryData == null)
			queryData = getQueryData();
		try {
			List<JSONObject> result = attendService.search(queryData, "attendance.select.managequery");
			queryResponse(queryData, result);
		} catch (Exception e) {
			LOG.error("查询考勤设备发生错误", e);
			json.put("status", "-2");
			json.put("msg", "查询考勤设备信息操作发生错误");
		} finally {
			try {
				response(json);
			} catch (IOException e) {
				LOG.error("查询返回考勤设备信息时发生错误", e);
			}
			queryData = null;
		}
	}
	
	@RequestMapping("queryAttend")
	public void queryAttend() {
		JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
		if (queryData == null)
			queryData = getQueryData();
		try {
			List<JSONObject> result = attendService.queryAttend(queryData);
			queryResponse(queryData, result);
		} catch (Exception e) {
			LOG.error("查询考勤设备发生错误", e);
			json.put("status", "-2");
			json.put("msg", "执行查询考勤设备操作发生错误");
		} finally {
			try {
				response(json);
			} catch (IOException e) {
				LOG.error("查询返回考勤设备信息时发生错误", e);
			}
			queryData = null;
		}
	}
	
	
	@RequestMapping("loadAttendance")
	public void loadAttendance() {
		JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
		try {
			JSONObject reqData = getReqData();
			JSONObject result = new JSONObject();
			attendService.delAttendTemp();
			result = attendService.loadAttendance(reqData);
			json.put("result", result);
			json.put("status", "1");
		} catch (Exception e) {
			LOG.error("下载考勤记录数据发生错误", e);
			json.put("status", "-2");
			json.put("msg", "下载考勤记录数据发生错误");
		} finally {
			try {
				response(json);
			} catch (IOException e) {
				LOG.error("返回信息时发生错误", e);
			}
		}
	}
	
	@RequestMapping("updateData")
	public void updateData() {
		JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
		try {
			attendService.updateAttend();
			json.put("status", "1");
		} catch (Exception e) {
			LOG.error("更新考勤记录数据时发生错误", e);
			json.put("status", "-2");
			json.put("msg", "更新考勤记录数据时发生错误");
		} finally {
			try {
				response(json);
			} catch (IOException e) {
				LOG.error("返回信息时发生错误", e);
			}
		}
	}
	
	@RequestMapping("searchMach/{snn}")
    public void searchMach(@PathVariable String snn){
        JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
        try {
            JSONObject result = attendService.searchMach(snn);
            json.put("data", result);
            json.put("status", 1);
        } catch (Exception e) {
            LOG.error("查询考勤设备：" + snn + "失败", e);
            json.put("status", "-2");
            json.put("msg", "查询考勤设备：" + snn + "发生错误");
        } finally {
            try {
                response(json);
            } catch (IOException e) {
                LOG.error("返回信息时发生错误", e);
            }
        }
    }
	
	 @RequestMapping("updateMachinfo")
	    public void updateMachinfo() {
	        JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
	        try {
	            JSONObject reqData = getReqData();
	            JSONObject result = new JSONObject();
	            result = attendService.updateMachinfo(reqData);
	            json.put("data", result);
	            json.put("status", "1");
	        } catch (Exception e) {
	            LOG.error("更新员工基本信息发生错误", e);
	            json.put("status", "-2");
	            json.put("msg", "更新员工基本信息发生错误");
	        } finally {
	            try {
	                response(json);
	            } catch (IOException e) {
	                LOG.error("返回信息时发生错误", e);
	            }
	        }
	    }
	 
	 
	 
	 @RequestMapping("deleteMachinfo/{machsn}")
	 public void deleteMachinfo(@PathVariable String machsn) throws Exception{
		 int status = attendService.deleteMachinfo(machsn);
		 JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
		 json.put("status", status);
		 response(json);
	 }
		


}
